package edu.mis.modules.project;

import java.util.List;

import com.jfinal.plugin.activerecord.Record;

import edu.mis.model.Project;

public class ProjectService {
	
	/**
	 * 添加项目
	 * @param project
	 * @return
	 */
	public int add(Project project){		
		if(project.findProjectByProjectNumber(project.getProjectNumber())!=null){
			if(project.findProjectByProjectName(project.getProjectName())!=null) {
				//项目名和项目编号重复
				return 0;
			}
			else {
				//项目编号重复
				return 1;
			}	
		}else {
			if(project.findProjectByProjectName(project.getProjectName())!=null) {
				//项目名重复
				return 2;
			}
			else {
				project.save();
				//添加项目成功
				return 3;
			}		
		}
		
	}
	
	/**
	 * 修改项目编号
	 * @param projectNumber
	 * @return
	 */
	public boolean updateProjectNumber(int projectId,int projectNumber){
		Project project = findProjectByProjectId(projectId);
		project.setProjectNumber(projectNumber);
		return project.update();
	}
	
	/**
	 * 根据ID得到项目
	 * @param projectId
	 * @return
	 */
	public Project findProjectByProjectId(int projectId) {
		return Project.dao.findById(projectId);
	}
	
	/**
	 * 查询所有项目
	 * @return
	 */
	public List<Project> findAllProject(){
		return Project.dao.findAll();
	}
	/**
	 * 查询所有项目及其类别
	 * @return
	 */
	public List<Record> findAllProjectWithProjectVariety(){
		return Project.dao.findAllRecord();
	}

	/**
	 * 项目授予类别
	 * @param projectId
	 * @param projectVarietyId
	 * @return
	 */
	public boolean ProjectAuthorize(int projectId, int projectVarietyId) {
		Project project = findProjectByProjectId(projectId);
		project.setProjectVarietyId(projectVarietyId);
		return project.update();
	}
	
	/**
	 * 删除项目
	 * @param projectId
	 * @return
	 */
	public boolean deleteProjectyByProjectId(int projectId) {
		return Project.dao.deleteById(projectId);
	}
}
